package capaPresentacion;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import capaDatos.Point;


public class CtrlMapa {
	private static CtrlMapa instancia; 
	Map<Integer, Point[]> servidores;
	
	private CtrlMapa() {
		servidores = new HashMap<Integer, Point[]>();
	}
	
	public static CtrlMapa getInstancia() {
		if(instancia == null) instancia = new CtrlMapa();
		
		return instancia;
	}

	public Iterator<Integer> getServidores() {
		Map<Integer, Point[]>  sc = null;
		synchronized(servidores) {
			sc = servidores;
		}
		return sc.keySet().iterator();
	}
	
	public Point[] getLocationServidor(int ip) {
		Point[] res = null;
		synchronized(servidores) { res = translate(servidores.get(ip));}
		return res;
	}

	private Point[] translate(Point[] points) {
		Point fin = points[1];
		Point ini = points[0];
		Point[] pMapa = {new Point(fin.x + ini.x, fin.y + ini.x), 
						 new Point(fin.x - ini.x, fin.y - ini.x)};		
		return pMapa;
	}

	public void altaServidor(double x, double y, double x2, double y2, int ip) {
		if(!servidores.containsKey(ip)) {
			Point[] p = {new Point(x, y),new Point(x2, y2)};
			servidores.put(ip, p);
		}
	}

	public void borrarServidor(int ip) {
		if(servidores.containsKey(ip)) {
			servidores.remove(ip);
		}
	}
	
	
}
